/*
 * ContatosTreeModel.java
 *
 * Created on October 28, 2009, 6:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package agendaBD;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author godinho
 */
public class ContatosTreeModel implements TreeModel{
    
    private RootCategory mainCategory = new RootCategory();
    
    public ContatosTreeModel(){
        
        Category cAmigos = new Category("Amigos");    
        Category cFamilia = new Category("Família");
        Category cProfissional = new Category("Profissional");
        Category cProfessor = new Category("Professor");

        this.mainCategory.addCategory(cAmigos);
        this.mainCategory.addCategory(cFamilia);
        this.mainCategory.addCategory(cProfissional);
        this.mainCategory.addCategory(cProfessor);
    }

    public Object getRoot() {
        return mainCategory;
    }

    public void addContato(Contato c, String categoria){

        Category cCategory = this.searchCategoria(categoria);

        if(cCategory != null){
            cCategory.addContato(c);
        }
    }

    public void delContato(Contato c){

        Category categoriaCorrente;

        for(int i = 0; i < this.mainCategory.getChildCount(); i++){

            categoriaCorrente = this.mainCategory.getCategory(i);

            for(int j = 0; j < categoriaCorrente.getContatoCount(); j++){

                if(categoriaCorrente.getContato(j).equals(c)){
                    categoriaCorrente.removeContato(c);
                }
            }
        }
    }

    public Category searchCategoria(String name){

        String current;

        for(int i = 0; i < this.mainCategory.getChildCount(); i++){

            current = this.mainCategory.getCategory(i).getCategoryName().toUpperCase();

            if(current.equals(name.toUpperCase())){
                return this.mainCategory.getCategory(i);
            }
        }

        return null;
    }

    public Object getChild(Object parent, int index) {
        
        if(parent instanceof Category){
            
            Category c = (Category) parent;
            
            return c.getContato(index);
        }
        else if(parent instanceof RootCategory){
            
            RootCategory rc = (RootCategory) parent;
            
            return rc.getCategory(index);
            
        }
        
        return null;
    }

    public int getChildCount(Object parent) {
        
        if(parent instanceof Category){
            Category c = (Category) parent;
               
            return c.getContatoCount();
            
        }
        else if(parent instanceof RootCategory){
            RootCategory rc = (RootCategory) parent;
            
            return rc.getChildCount();
        }
        
        return 0;
    }

    public boolean isLeaf(Object node) {
        return node instanceof Contato;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
    }

    public int getIndexOfChild(Object parent, Object child) {
        if(parent instanceof Category){
            Category c = (Category) parent;
            
            return c.indexOfContato((Contato)child);
        }
        else if(parent instanceof RootCategory){
            
            RootCategory rc = (RootCategory) parent;
            
            return rc.indexOf((Category)child);
            
        }
        
        return 0;
    }

    public void addTreeModelListener(TreeModelListener l) {
    }

    public void removeTreeModelListener(TreeModelListener l) {
    }
    
    protected class RootCategory {
        
        private List<Category> categorys = new ArrayList<Category>();
        
        public RootCategory(){
            
        }
        
        public void addCategory(Category c){
            this.categorys.add(c);
        }
        
        public Category getCategory(int index){
            return this.categorys.get(index);
        }
        
        public void removeCaregory(Category c){
            this.categorys.remove(c);
        }
    
        public int indexOf(Category c){
            return this.categorys.indexOf(c);
        }
        
        public int getChildCount(){
            return this.categorys.size();
        }
    }
    
}
